﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ProductReviewRepository : Data.Repository.IProductReviewRepository
    {
        private LinqToECommerceDBDataContext _dbContext;
        
        #region IProductReviewRepository Members

        public int CreateNewProductReview(Data.Entity.ProductReview theNewProductReview)
        {
            if (theNewProductReview == null)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductReview_Create(theNewProductReview.ProductID, theNewProductReview.UserID, theNewProductReview.IPAddress,
					theNewProductReview.Title, theNewProductReview.ReviewText, theNewProductReview.Rating, theNewProductReview.HelpfulYesTotal, theNewProductReview.HelpfulNoTotal,
					theNewProductReview.IsApproved, theNewProductReview.CreatedOn);
			}

			return result;
		}

		/// <summary>
		/// delete productreview method
		/// </summary>
		/// <param name="id"></param>
		/// <returns>0: failed 1: successed</returns>
		public int DeleteProductReview(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductReview_Delete(id);
			}

			return result;
		}

        public IList<Data.Entity.ProductReview> GetProductReviewsByProduct(int productID)
        {
            if (productID <= 0)
            {
                return null;
            }

			List<ECommerce_GetProductReviews_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProductReviews_By_Product(productID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductReview>();

				foreach(var pr in lstData)
				{
					var theEntity = new Data.Entity.ProductReview()
					{
						CreatedOn = pr.CreatedOn,
						HelpfulNoTotal = pr.HelpfulNoTotal,
						HelpfulYesTotal = pr.HelpfulYesTotal,
						IPAddress = pr.IPAddress,
						IsApproved = pr.IsApproved,
						ProductID = pr.ProductID,
						ProductReviewID = pr.ProductReviewID,
						Rating = pr.Rating,
						ReviewText = pr.ReviewText,
						Title = pr.Title,
						UserID = pr.UserID,
						ReplyText = pr.ReplyText,
						RepliedOn = pr.RepliedOn
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateProductReview(Data.Entity.ProductReview theProductReview)
		{
			if(theProductReview == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProductReview = (from pr in _dbContext.ProductReviews
									   where pr.ProductReviewID == theProductReview.ProductReviewID
									   select pr).FirstOrDefault();

				if(dbProductReview == null)
				{
					result = 0;
				}
				else
				{
					dbProductReview.CreatedOn = theProductReview.CreatedOn;
					dbProductReview.HelpfulNoTotal = theProductReview.HelpfulNoTotal;
					dbProductReview.HelpfulYesTotal = theProductReview.HelpfulYesTotal;
					dbProductReview.IPAddress = theProductReview.IPAddress;
					dbProductReview.IsApproved = theProductReview.IsApproved;
					dbProductReview.ProductID = theProductReview.ProductID;
					dbProductReview.Rating = theProductReview.Rating;
					dbProductReview.ReviewText = theProductReview.ReviewText;
					dbProductReview.Title = theProductReview.Title;
					dbProductReview.UserID = theProductReview.UserID;
					dbProductReview.ReplyText = theProductReview.ReplyText;
					dbProductReview.RepliedOn = theProductReview.RepliedOn;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public IList<Data.Entity.ProductReview> GetAllProductReviewsByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			List<Data.ProductReview> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var productIDs = (from p in _dbContext.Products
								  where p.LanguageID.Equals(languageID) && p.Published
								  select p.ProductID);

				var data = from pr in _dbContext.ProductReviews
						   where productIDs.Contains(pr.ProductID)
						   select pr;

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductReview>();

				foreach(var pr in lstData)
				{
					var theEntity = new Data.Entity.ProductReview()
					{
						CreatedOn = pr.CreatedOn,
						HelpfulNoTotal = pr.HelpfulNoTotal,
						HelpfulYesTotal = pr.HelpfulYesTotal,
						IPAddress = pr.IPAddress,
						IsApproved = pr.IsApproved,
						ProductID = pr.ProductID,
						ProductReviewID = pr.ProductReviewID,
						Rating = pr.Rating,
						ReviewText = pr.ReviewText,
						Title = pr.Title,
						UserID = pr.UserID,
						ReplyText = pr.ReplyText,
						RepliedOn = pr.RepliedOn
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
